package com.rms.recruit_management_system.controller;

import com.rms.recruit_management_system.pojo.Department;
import com.rms.recruit_management_system.service.DepartmentService;
import com.rms.recruit_management_system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/department/list")
    public Result<List<Department>> getDepartmentList() {
        String key="department_";
        List<Department> departmentList = (List<Department>) redisTemplate.opsForValue().get(key);
        redisTemplate.opsForValue().get(key);
        if(departmentList != null && departmentList.size()>0){
            return new Result<>(200,"SUCCESS",departmentList);
        }
        departmentList=departmentService.getDepartmentList(null);
        redisTemplate.opsForValue().set(key,departmentList,60, TimeUnit.SECONDS);
        return new Result<>(200,"SUCCESS",departmentList);

    }

    @RequestMapping("/department/add")
    public Result<?> addDepartment(@RequestBody Department department) {
        departmentService.addDepartment(department);
        cleanCache("department_*");
        return new Result<>(200,"SUCCESS",department);
    }

    @RequestMapping("/department/batchAdd")
    public Result<?> addBatchDepartment(@RequestBody List<Department> departmentList) {
        for(Department department:departmentList){
            department.setCreateTime(LocalDateTime.now());
            department.setUpdateTime(LocalDateTime.now());
        }
        departmentService.saveBatch(departmentList);
        cleanCache("department_*");
        return new Result<>(200,"SUCCESS",departmentList);
    }
    @RequestMapping("/department/update")
    public Result<?> updateDepartment(@RequestBody Department department) {
        departmentService.updateDepartment(department);
        cleanCache("department_*");
        return new Result<>(200,"SUCCESS",department);
    }
    @RequestMapping("/department/delete/{departmentId}")
    public Result<?> deleteDepartment(@PathVariable Integer departmentId) {
        departmentService.deleteDepartment(departmentId);
        cleanCache("department_*");
        cleanCache("positions_*");
        return new Result<>(200,"SUCCESS");
    }

    @RequestMapping("/department/batchDelete")
    public Result<?> deleteBatchDepartment(@RequestBody List<Integer> departmentIds) {
        departmentService.deleteBatchDepartment(departmentIds);
        cleanCache("department_*");
        cleanCache("positions_*");
        return new Result<>(200,"SUCCESS");
    }

    public void cleanCache(String pattern){
        Set keys=redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
